<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item style="display: flex; align-items: center;">
        <el-button icon="Refresh" @click="resetQuery" class="btn"></el-button>
        <el-form-item label="审批状态" prop="state" style="width: 220px;">
          <el-select v-model="queryParams.state" placeholder="请选择审批状态" clearable>
            <el-option
                v-for="dict in stateTypevalue"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆号码" prop="carNum" class="search">
          <el-input
              v-model="queryParams.carNum"
              placeholder="请输入车辆号码"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="真实姓名" prop="userName" class="search">
          <el-input
              v-model="queryParams.userName"
              placeholder="请输入真实姓名"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="身份证号" prop="identifyNum" class="search">
          <el-input
              v-model="queryParams.identifyNum"
              placeholder="请输入身份证号"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="申请时间" style="width: 290px;">
          <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery" class="btns"></el-button>
      </el-form-item>
    </el-form>


    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:cartaudit:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cartaudit:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cartaudit:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:cartaudit:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="cartauditList" @selection-change="handleSelectionChange">
      <el-table-column label="真实姓名" align="center" prop="userName" />
      <el-table-column label="身份证号" align="center" prop="identifyNum" />
      <el-table-column label="品牌图标" align="center" prop="brandImg" >
        <template #default="scope">
          <el-image style="width: 100px; height: 80px; display: block" v-if="scope.row.brandImg"
                    :src="scope.row.brandImg"
                    it="contain"
                    title="点击查看大图"
                    :preview-src-list="[scope.row.brandImg]"
                    preview-teleported
                    hide-on-click-modal>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="品牌名称" align="center" prop="brandName" />
      <el-table-column label="品牌型号" align="center" prop="brandModel" />
      <el-table-column label="车牌号码" align="center" prop="carNum" />
      <el-table-column label="交强险到期时间" align="center" prop="trafficTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.trafficTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商业险到期时间" align="center" prop="businessTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.businessTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最新申请时间" align="center" prop="applicationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applicationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="state" >
        <template #default="scope">
          <el-tag v-if="scope.row.state === '0'" type="primary">待审批</el-tag>
          <el-tag v-if="scope.row.state === '1'" type="success">通过</el-tag>
          <el-tag v-if="scope.row.state === '2'" type="danger">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审批时间" align="center" prop="auditTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width"  fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" v-if="scope.row.state == 0" @click="handleUpdate(scope.row),isShow(true)"

                     v-hasPermi="['system:cartaudit:edit']">审批
          </el-button>
          <el-button link type="primary" icon="View" v-if="scope.row.state == 1" @click="handleUpdate(scope.row),isShow(false)"
                     v-hasPermi="['system:cartaudit:view']">查看
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:cartaudit:remove']">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆认证对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="cartauditRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="品牌图标" prop="brandImg">
          <el-image style="width: 100px; height: 80px;" v-if="form.brandImg"
                    :src="form.brandImg" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.brandImg]" preview-teleported hide-on-click-modal>
          </el-image>
        </el-form-item>
        <el-form-item label="品牌名称" prop="brandName">
          <el-input disabled v-model="form.brandName" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="品牌型号" prop="brandModel">
          <el-input disabled v-model="form.brandModel" placeholder="请输入品牌型号" />
        </el-form-item>
        <el-form-item label="司机姓名" prop="userName">
          <el-input disabled v-model="form.userName" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="车牌号码" prop="carNum">
          <el-input disabled v-model="form.carNum" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="交强险合同照片" prop="trafficImg">
          <el-image style="width: 100px; height: 80px;" v-if="form.trafficImg"
                    :src="form.trafficImg" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.trafficImg]" preview-teleported hide-on-click-modal>
          </el-image>
        </el-form-item>

        <el-form-item label="交强险到期时间" prop="trafficTime">
          <el-date-picker
            v-model="form.trafficTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="商业险合同照片" prop="businessImg">
          <el-image style="width: 100px; height: 80px;" v-if="form.businessImg"
                    :src="form.businessImg" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.businessImg]" preview-teleported hide-on-click-modal>
          </el-image>
        </el-form-item>
        <el-form-item label="商业险到期时间" prop="businessTime">
          <el-date-picker
            v-model="form.businessTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          />
        </el-form-item>

        <el-form-item label="审批处理" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" >
          <el-button type="primary" @click="submitForm" v-if="flag===1">确 定</el-button>
          <el-button @click="cancel" v-if="flag===1">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cartaudit">
import { listCartaudit, getCartaudit, delCartaudit, addCartaudit, updateCartaudit } from "@/api/system/cartaudit";

const { proxy } = getCurrentInstance();

const cartauditList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const flag = ref(0);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    carNum: null,
    applicationData: null,
    state: null,
  },
  rules: {
    driverId: [
      { required: true, message: "司机ID不能为空", trigger: "blur" }
    ],
    carNum: [
      { required: true, message: "车辆号码不能为空", trigger: "blur" }
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

const stateTypevalue = ref([
  {
    label: "待审批",
    value: 0
  },
  {
    label: "通过",
    value: 1
  },
  {
    label: "驳回",
    value: 2
  },
])

/** 查询车辆认证列表 */
function getList() {
  loading.value = true;
  listCartaudit(proxy.addDateRange(queryParams.value,dateRange.value)).then(response => {
    cartauditList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    authenticId: null,
    driverId: null,
    carId: null,
    carNum: null,
    trafficTime: null,
    businessTime: null,
    applicationData: null,
    state: null,
    trafficImg: null,
    businessImg: null,
    auditTime: null
  };
  proxy.resetForm("cartauditRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.authenticId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加车辆认证";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _authenticId = row.authenticId || ids.value
  getCartaudit(_authenticId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改车辆认证";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["cartauditRef"].validate(valid => {
    if (valid) {
      if (form.value.authenticId != null) {
        updateCartaudit(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCartaudit(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _authenticIds = row.authenticId || ids.value;
  proxy.$modal.confirm('是否确认删除车辆认证编号为"' + _authenticIds + '"的数据项？').then(function() {
    return delCartaudit(_authenticIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/cartaudit/export', {
    ...queryParams.value
  }, `cartaudit_${new Date().getTime()}.xlsx`)
}
//是否能继续修改
const isShow = (flagStatus) => {
  if(flagStatus){
    flag.value=1;
  }else{
    flag.value=0;
  }
}
getList();
</script>

<style scoped lang="scss">
   .btn{
    background-color: rgb(50,50,50);
    color: white;
    width: 37px;
     margin-right: 30px;
   }
   .btns{
    background-color: rgb(230,230,230);
    color: black;
    width: 37px;
    font-weight: bold;
    height: 30px;
    border: none;
    margin-left: -25px;
   }
   //.search{
   //  margin-left: 41%;
   //}

   .search{
     margin-left: 10px;
   }
   .app-container {
     .el-form-item {
       display: flex;
       flex-wrap: nowrap; // 防止元素换行
       align-items: center; // 垂直居中元素
     }
   }

</style>
